package com.sixi.GeoTransfer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



public class KmlAnalyseNeiMeng {

	public static void main(String[] args) {

		try {
			
			 //***************初始化文件流
			 File f = new File("C:\\Users\\ThinkPad\\Desktop\\内蒙FZ\\内蒙古自治区_行政边界\\neimeng_border.kml");
			 //**************输出文件流
			 FileOutputStream out = null;
			 out = new FileOutputStream(new File("./src/main/resources/neimeng_city_info.txt"));
				//***************建立dom解析模式
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				
				//***************初始化文件流
				Document doc = builder.parse(f);
				
				NodeList nodeList = doc.getElementsByTagName("Placemark");
				
				
				for(int i = 0 ; i < nodeList.getLength() ; i ++) {
					                                            //coordinates/LinearRing/outerBoundaryIs/Polygon/MultiGeometry/Placemark
					NodeList placemarkChildNodes = nodeList.item(i).getChildNodes();
					
					String areaName = ""; //区域名称
					String areaOutply = ""; //区域图形
					
					for(int j = 0 ;j < placemarkChildNodes.getLength() ; j ++) {
						Node placemarkChildNode = placemarkChildNodes.item(j);
						String nodeName = placemarkChildNode.getNodeName();
						
						switch (nodeName) {
							case "name" :
								areaName = placemarkChildNode.getTextContent().trim();
								break;
							case "MultiGeometry" : //coordinates/LinearRing/outerBoundaryIs/Polygon/MultiGeometry/Placemark
								NodeList multiGeometryChildNodes = placemarkChildNode.getChildNodes();
								for(int z = 0 ;z < multiGeometryChildNodes.getLength() ; z ++){
									Node multiGeometryChildNode = multiGeometryChildNodes.item(z);
									if(multiGeometryChildNode.getNodeName().equals("Polygon")){
										write1PolygonLine(multiGeometryChildNode, areaName, out);
									}
								}
								break;
							case "Polygon" : //coordinates/LinearRing/outerBoundaryIs/Polygon/Placemark
								write1PolygonLine(placemarkChildNode, areaName, out);
								break;
							default :
								break;
								
						}
						
						
					}

					
				}
			 
				out.close();
				
				
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

	
	public static void write1PolygonLine(Node polygonNode, String areaName, FileOutputStream out ) throws UnsupportedEncodingException, IOException{
		
		Node coordinates = polygonNode.getFirstChild().getNextSibling().getFirstChild().getNextSibling().getFirstChild().getNextSibling();
		//                    polygon/text             outerBoundaryIs/text             LinearRing      /text            coordinates
		
		String[] points = coordinates.getTextContent().split("\n");
		String outply = "";
		for(String pt : points) {
			if(pt.length() >= 2) {
				outply += pt.trim().substring(0, pt.length()-2) +  ";";
			}
		}
		
		String outPutStr = areaName + "\t" + outply.substring(0, outply.length()-1) + "\r\n";
		out.write(outPutStr.getBytes("utf-8"));
		
	}
	
}
